package com.thinkfit.jy.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.common.lib.bean.ApplicationBean;
import com.common.lib.manager.DataManager;
import com.common.lib.utils.BaseUtils;
import com.thinkfit.jy.R;
import com.thinkfit.jy.utils.ImageLoadUtils;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;


public class ApplicationAdapter extends BaseQuickAdapter<ApplicationBean, BaseViewHolder> {

    private Context mContext;
    private int mCurrentStyle;
    private int mProgressMargin;

    int mType;
    public static final int HOME = 1;
    public static final int APPLICATION = 2;

    public ApplicationAdapter(Context context,int type) {
        super(type == HOME ? R.layout.item_application_home:R.layout.item_application);
        mContext = context;
        mType = type;
        mCurrentStyle = DataManager.Companion.getInstance().getMainStyle();
        mProgressMargin = BaseUtils.StaticParams.dp2px(mContext, 2);
    }


    @SuppressLint("MissingPermission")
    @Override
    protected void convert(@NotNull BaseViewHolder helper, ApplicationBean bean) {
        if(TextUtils.isEmpty(bean.getAppid())){
//            helper.setBackgroundResource(R.id.fl_bg, R.drawable.shape_gradient_5f5c5f_3f3d3f_180);
            helper.setGone(R.id.ll_more,false);
            ArrayList<ImageView> imageViewList = getImageViewList(helper);
            for (int i=0;i<imageViewList.size();i++){
                ImageLoadUtils.getInstance(getContext()).setImageUtlRadius(imageViewList.get(i),getData().get(i).getIcon(),0,4);
            }
            helper.setText(R.id.tvName, mContext.getString(R.string.app_application_more));
            helper.setGone(R.id.shaderView,true);
        }else {

//            BaseUtils.StaticParams.loadImage(mContext, 0, bean.getIcon(), helper.getView(R.id.ivIcon));
            ImageLoadUtils.getInstance(getContext()).setImageUtlRadius(helper.getView(R.id.ivIcon),bean.getIcon(),0,22);

            if(mType == HOME){

            }else {
//                helper.setBackgroundResource(R.id.ivIcon, mCurrentStyle == 3 ?
//                                R.drawable.shape_33ffffff_6 : R.drawable.shape_gradient_5f5c5f_3f3d3f_180);
                helper.setGone(R.id.ivUninstall, bean.getInstallStatus() != 2);
            }
            helper.setGone(R.id.shaderView, bean.getInstallStatus() == 2);
            helper.setText(R.id.tvName, bean.getAppname());
            helper.setGone(R.id.ll_more,true);

            if (bean.getDownloadStatus() == 1) {   //下载中
                helper.setVisible(R.id.flProgress, true);
                int progress = bean.getProgress();
                if (progress > 100) {
                    progress = 100;
                }
                View flProgress = helper.getView(R.id.flProgress);
                if (flProgress.getWidth() != 0 || progress != 0) {
                    int dx = (int) ((flProgress.getWidth() - mProgressMargin) * 0.01 * progress + 0.5);
                    View progressView = helper.getView(R.id.progressView);
                    FrameLayout.LayoutParams lp1 = (FrameLayout.LayoutParams) progressView.getLayoutParams();
                    lp1.width = dx;
                    progressView.setLayoutParams(lp1);
                }
            } else {
                helper.setVisible(R.id.flProgress, false);
            }

        }

    }

    /**
     * 安装成功
     * @param appId
     */
    public void installSuccess(String appId) {
        List<ApplicationBean> list = getData();
        for (ApplicationBean bean : list) {
            if (bean.getAppid().equals(appId) ||
                    ("package:" + bean.getAppid()).equals(appId)) {
                bean.setInstallStatus(2);
                notifyDataSetChanged();
                break;
            }
        }
    }

    /**
     * 未安装成功
     * @param appId
     */
    public void unInstallSuccess(String appId) {
        List<ApplicationBean> list = getData();
        for (ApplicationBean bean : list) {
            if (bean.getAppid().equals(appId) ||
                    ("package:" + bean.getAppid()).equals(appId)) {
                bean.setInstallStatus(0);
                bean.setDownloadStatus(0);
                bean.setProgress(0);
                notifyDataSetChanged();
                break;
            }
        }
    }

    public ArrayList<ImageView> getImageViewList(BaseViewHolder helper){
        ArrayList<ImageView> list = new ArrayList<>();
        list.add(helper.getView(R.id.iv_1));
        list.add(helper.getView(R.id.iv_2));
        list.add(helper.getView(R.id.iv_3));
        list.add(helper.getView(R.id.iv_4));
        return list;
    }
}
